package com.glut.gultguy.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.glut.gultguy.To.ArticleTo;
import com.glut.gultguy.entity.Article;
import com.glut.gultguy.mapper.ArticleMapper;
import com.glut.gultguy.service.ArticleService;
import com.glut.gultguy.vo.ArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-09-05
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired(required = false)
    private ArticleMapper mapper;
    @Autowired
    private ArticleService service;

    @PostMapping("/getAllArticle/{page}")
    @ResponseBody
    public List<ArticleVo> getArticle(@PathVariable("page") Integer page){
       return service.getArticle(page);
    }

    @GetMapping("/getArticle/{id}")
    public ArticleVo getArticle(@PathVariable("id") String articleId){
        return service.getOneArticle(articleId);
    }
    @PostMapping("/publishArticle")
    @ResponseBody
    public Map<String, Integer> publish(@RequestBody ArticleTo to){
        return service.addArticle(to);
    }

    @PostMapping("/myArticle/{openid}")
    public List<ArticleVo> myArticles(@PathVariable("openid") String openid){
        return service.getMyArticles(openid);
    }

    @PostMapping("/setLock/{articleId}")
    public boolean setLock(@PathVariable("articleId") String id){
        this.mapper.updateLock(id,true);
        return true;
    }
    @PostMapping("/unLock/{articleId}")
    public boolean upLock(@PathVariable("articleId") String id){
        this.mapper.updateLock(id,false);
        return false;
    }
    //增加点击量
    @PostMapping("/click/{id}")
    public void addClick(@PathVariable("id") String articleId){
        mapper.addClick(articleId);
    }

}

